package com.zq.jdbc;

import java.sql.*;

/**
 * 演示依赖注入的原因
 */
public class JdbcDemo {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1加载数据库连接对象（省略）
//        Class.forName("com.mysql.cj.jdbc.Driver");
        //2创建数据库连接对象
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/zq", "root", "root");
        //3编写sql
        String sql = "select * from student3";
        //4创建语句执行对象
        preparedStatement = connection.prepareStatement(sql);
        //5设置参数
        //6执行sql返回结果集
        resultSet = preparedStatement.executeQuery(sql);
        ///7处理结果集
        while (resultSet.next()) {
            System.out.print(resultSet.getInt("id") + "\t");
            System.out.print(resultSet.getString("name") + "\t");
        }
        //8释放资源

    }
}
